function Curry(fn) {
    return function carryFn(...args) {
        if (args.length == fn.length) {
            return fn(...args)
        } else {
            return function () {
                return carryFn(...args.concat(Array.from(arguments)))
            }
        }
    }
}

function add(a, b, c, d) {
    return a + b + c + d;
};

var newAdd = Curry(add);
console.log(newAdd(1)(2)(3)(4))
console.log(newAdd(1, 2)(3, 4));
console.log(newAdd(1, 2, 3)(4));

// 函数组合
function compose(...arg) {
    return function (value) {
        // reduce 第一个参数是 上一步的返回值  下一个参数是下一项
        return arg.reduce((pre, next) => {
            return next(pre)
        }, value)
    }
}

function compose(...functions) {
    if (Array.isArray(functions) && functions.length > 0) {
      return functions.reduce((res, fn) => (...args) => fn(res(...args)))
    }
    if (Array.isArray(functions) && functions.length === 1) {
      return functions[0]
    }
    return () => new Error('compose函数接收的参数错误')
  }
  
const add = (x, y) => x + y
const square = (z) => z * z
const test1 = () => square(add(1, 2))
console.log(test1()) // 9